package com.dal.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 测试类
 * @author jian
 */
public class Demo02 {
    private static  Integer a = 0;
    private static  Integer b = 0;
    private static Integer c = 0;
    public static void main(String[] args) {

        //窗口01
        new Thread(() -> {
            while (true) {
                //获取当前票余量
                int currentTickets = TicketSource.getCurrentTickets();
                if (currentTickets > 0) {
                    TicketSource.saleTickets();
                    //waitProcess();
                    a++;
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    stopSale(a);
                    break;
                }
            }
        }, "01").start();

        //窗口02
        new Thread(() -> {
            while (true) {
                int currentTickets = TicketSource.getCurrentTickets();
                if (currentTickets > 0) {
                    TicketSource.saleTickets();
                    b ++;
                    //waitProcess();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    stopSale(b);
                    break;
                }
            }
        }, "02").start();

        //窗口03
        new Thread(() -> {
            while (true) {
                int currentTickets = TicketSource.getCurrentTickets();
                if (currentTickets > 0) {
                    TicketSource.saleTickets();
                    //waitProcess();
                    c++;
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    stopSale(c);
                    break;
                }
            }
        }, "03").start();


    }

    //模拟售票员卖出一张票用时且假设每个售票员业务能力相同
    private static void waitProcess() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    //停止售卖
    private static void stopSale(Integer count) {
        System.out.println(Thread.currentThread().getName() + "窗口：今日票已卖完！" + "卖了" +count+"张");
    }
}

/**
 * 电影票资源类
 */
class TicketSource {

    //当前电影票余量
    private static int currentTickets = 30;

    //加锁确保多线程场景下的线程安全
    private static Lock lock = new ReentrantLock();

    /**
     * 卖票
     *
     * @return 当前电影票余量
     */
    public static void saleTickets() {
        lock.lock();
        try {
            if (currentTickets > 0) {
                //模拟卖票
                currentTickets--;

                System.out.println(
                        Thread.currentThread().getName() + "窗口出票成功！"
                                + "当前票余量：" + currentTickets);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public static int getCurrentTickets() {
        return currentTickets;
    }
}
